package com.eims.sls_app.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.eims.sls_app.fragment.IntegralFragment;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.AppTools;
import com.eims.sls_app.utils.RequestUtil;
import com.eims.sls_app.view.MyToast;

import org.json.JSONObject;

import bls.juejin.cn.R;
import butterknife.BindView;

public class IntegralCenterAcitvity extends MyFragmentActivity implements
        IntegralFragment.OnFragmentInteractionListener {
    private Context context = IntegralCenterAcitvity.this;
    private static final String TAG = "IntegralDetailActivity";
    private FragmentTransaction transaction;
    private String exchangeScale;

    @BindView(R.id.btn_back)
    ImageButton btn_back;

    @BindView(R.id.btn_integral_exchange)
    Button btn_integral_exchange;

    @BindView(R.id.btn_integral_detail)
    Button btn_integral_detail;

    @BindView(R.id.tv_integral_tip)
    TextView tv_integral_tip;

    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.layout_integral_detail);
        // ButterKnife.inject(this);
        init();
    }

    @Override
    public Resources getResources() {
        // TODO Auto-generated method stub
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }

    /**
     * 跳转到积分明细
     */
    // @OnClick(R.id.btn_integral_detail)
    protected void onclickDetail() {
        intent = new Intent(this, IntegralDetailActivity.class);
        this.startActivity(intent);
    }

    public void onclickDetail(View view) {
        intent = new Intent(this, IntegralDetailActivity.class);
        this.startActivity(intent);
    }

    /**
     * 兑换积分
     */
    // @OnClick(R.id.btn_integral_exchange)
    // protected void onclickExchange() {
    // intent = new Intent(this, IntegralExchangeActivity.class);
    // this.startActivity(intent);
    // }
    public void onclickExchange(View view) {
        intent = new Intent(this, IntegralExchangeActivity.class);
        this.startActivity(intent);
    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    private void init() {


        // 初始化控件
        initView();

        RequestUtil requestUtil = new RequestUtil(context, false, 0, false,
                "正在加载...") {
            @Override
            public void responseCallback(JSONObject reponseJson) {
                if (RequestUtil.DEBUG)
                    Log.i(TAG, "获取积分参数返回结果：" + reponseJson);
                String error = reponseJson.optString("error");
                if (null != error && error.equals("0")) {// 正常返回
                    App.currentScoring = reponseJson
                            .optInt("currentScoring");
                    App.totalScoring = reponseJson.optInt("totalScoring");
                    App.totalConversionScoring = reponseJson
                            .optInt("totalConversionScoring");
                    App.scoringExchangerate = reponseJson
                            .optInt("scoringExchangerate");
                    App.optScoringOfSelfBuy = reponseJson
                            .optInt("optScoringOfSelfBuy");
                    exchangeScale = App.scoringExchangerate + "";
                } else {
                    App.currentScoring = 0;
                    App.totalScoring = 0;
                    App.totalConversionScoring = 0;
                    App.scoringExchangerate = 0;
                    App.optScoringOfSelfBuy = 0;
                    exchangeScale = App.scoringExchangerate + "";
                    String msg = reponseJson.optString("msg");
                    if (RequestUtil.DEBUG)
                        Log.e(TAG, "积分参数请求错误" + msg);
                    MyToast.getToast(context, msg).show();
                }
                setParams();
            }

            @Override
            public void responseError(VolleyError error) {
                App.currentScoring = 0;
                App.totalScoring = 0;
                App.totalConversionScoring = 0;
                App.scoringExchangerate = 0;
                App.optScoringOfSelfBuy = 0;
                exchangeScale = App.scoringExchangerate + "";
                setParams();
                if (RequestUtil.DEBUG)
                    Log.e(TAG, "积分参数请求错误" + error.getClass().getName() + "");
                MyToast.getToast(context, "数据请求错误，请稍后重试...").show();
            }
        };
        requestUtil.getIntegralParams();
        App.activityS.add(this);
        App.activityS1.add(this);
    }

    private void initView() {
        btn_back = (ImageButton) findViewById(R.id.btn_back);

        btn_integral_exchange = (Button) findViewById(R.id.btn_integral_exchange);

        btn_integral_detail = (Button) findViewById(R.id.btn_integral_detail);

        tv_integral_tip = (TextView) findViewById(R.id.tv_integral_tip);

    }

    // @OnClick(R.id.btn_back)
    protected void onclickBack() {
        for (Activity activity : App.activityS1) {
            activity.finish();
        }
    }

    // 返回
    public void onclickBack(View view) {
        for (Activity activity : App.activityS1) {
            activity.finish();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onclickBack();
        }
        return super.onKeyDown(keyCode, event);
    }

    public void setParams() {
        transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.frame_integral_detail, IntegralFragment
                .newInstance(App.currentScoring + "",
                        App.totalScoring + "",
                        App.totalConversionScoring + ""));
        transaction.commit();
        Spanned tip = Html
                .fromHtml("<font color='#000000'>我的积分规则:</font><br/>"
                        + "<font color='#000000'>1. 我的积分是对用户参与本站cp代购、合买进行奖励的积分机制。</font><br/>"
                        + "<font color='#000000'>2. 我的购彩积分：</font><br/>"
                        + "    <font color='#000000'>在本站参与代购、合买cp的用户，每成功购买满 1 元（撤单、方案未成功不积分），积</font> "
                        + AppTools.changeStringColor("#e3393c",
                        App.optScoringOfSelfBuy + "")
                        + " <font color='#000000'>分，单次投注不满 1 元不积分。</font><br/>"
                        + "<font color='#000000'>3.我的中奖积分：</font><br/>"
                        + "    <font color='#000000'>本站后台根据不同彩种以及不同玩法设置了一定的积分比例，代购或者参与合买的用户，将根据奖金的金额比例获得对应的积分。</font><br/>"
                        + "<font color='#000000'>4.积分兑换：</font><br/>"
                        + "    <font color='#000000'>积分满</font> "
                        + AppTools.changeStringColor("#e3393c", exchangeScale
                        + "")
                        + " <font color='#000000'>分，用户可以进行积分兑换，兑换以</font> "
                        + AppTools.changeStringColor("#e3393c", exchangeScale
                        + "")
                        + " <font color='#000000'>分为一个兑换单位，超过</font> "
                        + AppTools.changeStringColor("#e3393c", exchangeScale
                        + "")
                        + " <font color='#000000'>分兑换奖励的用户，兑换积分必须是</font> "
                        + AppTools.changeStringColor("#e3393c", exchangeScale
                        + "")
                        + " <font color='#000000'>的整数倍，不够</font> "
                        + AppTools.changeStringColor("#e3393c", exchangeScale
                        + "")
                        + " <font color='#000000'>分不能兑换。</font>"
                        + AppTools.changeStringColor("#e3393c", exchangeScale
                        + "")
                        + " <font color='#000000'>分兑换 1 元，兑换后此款项自动增加到用户的可用资金中，但不能对积分兑换的金额进行提款。</font>");
        // Spanned tip = Html.fromHtml("我的积分规则:<br/>" +
        // "1. 我的积分是对用户参与本站cp代购、合买进行奖励的积分机制。<br/>" +
        // "2. 我的购彩积分：<br/>" +
        // "    在本站参与代购、合买cp的用户，每成功购买满 1 元（撤单、方案未成功不积分），积 "+App.changeStringColor("#e3393c",App.optScoringOfSelfBuy+""
        // )+" 分，单次投注不满 1 元不积分。<br/>" +
        // "3.我的中奖积分：<br/>" +
        // "    本站后台根据不同彩种以及不同玩法设置了一定的积分比例，代购或者参与合买的用户，将根据奖金的金额比例获得对应的积分。<br/>"
        // +
        // "4.积分兑换：<br/>" +
        // "    积分满 " + App.changeStringColor("#e3393c",exchangeScale+"" )
        // +
        // " 分，用户可以进行积分兑换，兑换以 " +
        // App.changeStringColor("#e3393c",exchangeScale+"" ) +
        // " 分为一个兑换单位，超过 " +
        // App.changeStringColor("#e3393c",exchangeScale+"" ) +
        // " 分兑换奖励的用户，兑换积分必须是 " +
        // App.changeStringColor("#e3393c",exchangeScale+"" ) +
        // " 的整数倍，不够 " + App.changeStringColor("#e3393c",exchangeScale+"" )
        // +
        // " 分不能兑换。" + App.changeStringColor("#e3393c",exchangeScale+"" ) +
        // " 分兑换 1 元，兑换后此款项自动增加到用户的可用资金中，但不能对积分兑换的金额进行提款。");
        tv_integral_tip.setText(tip);
    }

    @Override
    public void onFragmentInteraction(Uri uri) {
        //onclickDetail();
    }
}
